package androidtool.checkconvention;

import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Element;

/**
 *
 * @author Hoang
 */
public class Widget {

    private String type;
    private String prefix;
    private String id;
    private String correctId;
    private boolean corrected;
    private boolean selected;

    public Widget(Element element) {
        type = element.getName();

        if (type.contains(".")) {
            int temp = 0, count = 0;
            int size = type.length();

            while (count < size) {
                if (type.charAt(count) == '.') {
                    temp = count;
                }
                count++;
            }
            type = type.substring(temp + 1);
        }
        selected = true;

        List<Attribute> listAttribute = element.attributes();
        setId(listAttribute);

        if (id != null) {

            if (type.contains("Layout")) {
                prefix = "layout";

            } else if (type.contains("TextView")) {
                prefix = "lbl";

            } else if (type.contains("Button")) {
                prefix = "btn";

            } else if (type.contains("EditText")) {
                prefix = "txt";

            } else if (type.contains("ImageView")) {
                prefix = "img";

            } else if (type.contains("ListView")) {
                prefix = "lsv";

            } else if (type.contains("CheckBox")) {
                prefix = "chk";

            } else if (type.contains("RadioBox")) {
                prefix = "rdb";

            } else if (type.contains("ScrollView")) {
                prefix = "scv";

            } else {
                prefix = type.toLowerCase();
            }

            if ((id.length() > prefix.length()) && (id.substring(0, prefix.length()).equals(prefix))) {
                correctId = id;
                corrected = true;
            } else {
                correctId = prefix + getUpperCaseId();
                corrected = false;
            }
        }
    }

    private void setId(List<Attribute> listAttribute) {
        for (Attribute attribute : listAttribute) {
            if (attribute.getName().equalsIgnoreCase("id")) {
                id = attribute.getValue().substring(5);
                return;
            }
        }
        id = null;
    }

    public String getType() {
        return type;
    }

    public String getId() {
        return id;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    public String getCorrectId() {
        return correctId;
    }

    public boolean isCorrected() {
        return corrected;
    }

    public String getOnClick() {
        return "onClick" + id.substring(prefix.length()) + "()";
    }

    @Override
    public String toString() {
        return ("type: " + type + ", id: " + id);
    }

    private String getUpperCaseId() {
        return (id.substring(0, 1).toUpperCase() + id.substring(1));
    }
}
